package com.wlc.io;

import java.io.*;
import java.nio.charset.Charset;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description 字符输入流
 * @date 2021-07-09 12:48
 */
public class ReaderDemo {


    public static void fileReader() {
        File file = new File("/Users/wanglichao/Desktop/temp/a.txt");
        try (FileReader fileReader = new FileReader(file)) {
            char[] chars = new char[(int) file.length()];
            fileReader.read(chars);
            for (char c : chars) {
                System.out.print(c);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("");

    }

    public static void inputStream() {
        File file = new File("/Users/wanglichao/Desktop/temp/a.txt");
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            byte[] chars = new byte[(int) file.length()];
            fileInputStream.read(chars);
            for (byte c : chars) {
                System.out.print(c);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 使用 fileWriter 字符输出流，把字符输出到磁盘上面
     */
    public static void fileWriter() {
        File file = new File("/Users/wanglichao/Desktop/temp/b.txt");
        try (FileWriter fileWriter = new FileWriter(file)) {
            String data = "我";
            fileWriter.write(data);
            System.out.println("写入完毕。。");
        } catch (IOException e) {
            System.out.println("写入文件发生异常，原因是：" + e.getMessage());
        }
    }

    /***
     * 解决汉字编码问题
     */
    public static void FileInputStream1() {
        File file = new File("/Users/wanglichao/Desktop/temp/b.txt");
        // 使用字节流读取文件
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            byte[] bytes = new byte[(int) file.length()];
            fileInputStream.read(bytes);
            for (byte b : bytes) {
                System.out.println("字节流读取出来的内容是:" + b);
                int i = b & 0x000000ff;  //只取16进制的后两位
                System.out.println(Integer.toHexString(i));
                System.out.println("把这个数字，放在GBK的棋盘上去：");
                String str = new String(bytes, "UTF-8");
                System.out.println(str);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test2() {
        // 使用FileReader 不可以直接对字符设置编码；
        // 使用inputStreamReader 可以设置字符的编码
        File file = new File("/Users/wanglichao/Desktop/temp/b.txt");
        try {
            FileReader fileReader = new FileReader(file);
            char[] chars = new char[(int) file.length()];
            fileReader.read(chars);
            System.out.printf("FileReader会使用默认的编码方式%s,识别出来的字符是：%n", Charset.defaultCharset());
            System.out.println("chars = " + new String(chars));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //FileReader是不能手动设置编码方式的，为了使用其他的编码方式，只能使用InputStreamReader来代替
        //并且使用new InputStreamReader(new FileInputStream(f),Charset.forName("UTF-8")); 这样的形式
        try (InputStreamReader isr = new InputStreamReader(new FileInputStream(file),Charset.forName("UTF-8"))) {
            char[] cs = new char[(int) file.length()];
            isr.read(cs);
            System.out.printf("InputStreamReader 指定编码方式UTF-8,识别出来的字符是：%n");
            System.out.println(new String(cs));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        test2();
    }
}
